<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="cn.estore.entity.Product" %>
<%@ page import="cn.estore.dao.ProductDao" %>
<%@ page import="java.sql.SQLException" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>按商品名称查询商品信息展示</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f8f9fa;
			margin: 0;
			padding: 0;
			text-align: center;
		}

		header {
			background-color: #343a40;
			color: white;
			padding: 20px 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 20px;
			padding-right: 20px;
		}

		header a {
			color: white;
			text-decoration: none;
			margin: 0 15px;
		}

		header span {
			color: white;
			font-weight: bold;
		}

		main {
			padding: 20px;
		}

		table {
			width: 80%;
			margin: 20px auto;
			border-collapse: collapse;
			box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
		}

		th, td {
			padding: 15px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}

		img {
			max-width: 100%;
			max-height: 150px;
			display: block;
			margin: 0 auto;
		}

		.product-info {
			padding-left: 20px;
		}

		/* 用于隐藏元素的CSS类 */
		.hide {
			display: none;
		}
	</style>
</head>
<body>
<header>
	<%
		String username = "";
		// 检查Cookie中是否存在username，以此判断用户是否登录
		boolean isLoggedIn = false;
		Cookie[] cookies = request.getCookies();
		if (cookies!= null) {
			for (Cookie cookie : cookies) {
				if ("username".equals(cookie.getName())) {
					username = cookie.getValue();
					isLoggedIn = true;
					break;
				}
			}
		}

		if (isLoggedIn) {
			// 如果已登录，为登录和注册按钮添加hide类，使其隐藏，并在欢迎语后显示用户名
			out.println("<a href='login.jsp' class='hide'>登录</a>");
			out.println("<a href='zhuce.jsp' class='hide'>注册</a>");
	%>
	<span id="loggedInUser">欢迎您，<%=username%></span>
	<%
		} else {
			// 如果未登录，正常输出登录和注册按钮的HTML代码
			out.println("<a href='login.jsp'>登录</a>");
			out.println("<a href='zhuce.jsp'>注册</a>");
		}
	%>
</header>
<main>
	<%
		request.setCharacterEncoding("UTF-8");
		// 获取用户输入的商品名称参数
		String name = request.getParameter("name");
		// 简单校验参数是否为空，如果为空给出提示信息并停止后续操作
		if (name == null || name.trim().isEmpty()) {
			out.println("<p>请输入有效的商品名称进行查询。</p>");
			return;
		}

		ProductDao dao = new ProductDao();
		List<Product> ProductList = null;
		// 调用数据库查询方法获取对应商品名称的商品列表
		ProductList = dao.selectProductsByName(name);

		if (ProductList!= null &&!ProductList.isEmpty()) {
			for (Product e : ProductList) {
	%>
	<table border="1" align="center">
		<tr>
			<td width="40%">
				<img src="<%="productImages/" + e.getPicture()%>" alt="商品图片">
			</td>
			<td class="product-info">
				<div>商品：<%=e.getName()%></div>
				<div>价格：<%=e.getMarketPrice()%>元</div>
				<div>简介：<%=e.getDescription()%></div>
				<div>登录才能购买</div>
			</td>
		</tr>
	</table>
	<%
			}
		} else {
			out.println("<p>未查询到名称为 " + name + " 的商品信息。</p>");
		}
	%>
</main>
</body>
</html>
